{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-09-05T02:06:21.159269Z",
     "start_time": "2025-09-05T02:06:21.153699Z"
    }
   },
   "source": [
    "import os\n",
    "\n",
    "from pandas.conftest import axis_1\n",
    "\n",
    "os.makedirs(os.path.join('.', 'data'), exist_ok=True)\n",
    "data_file = os.path.join('.', 'data', 'house_tiny.csv')\n",
    "# with open(data_file, 'w') as f:\n",
    "#     f.write('NumRooms,Alley,Price\\n')  # 列名\n",
    "#     f.write('NA,Pave,127500\\n')  # 每行表示一个数据样本\n",
    "#     f.write('2,NA,106000\\n')\n",
    "#     f.write('4,NA,178100\\n')\n",
    "#     f.write('NA,NA,140000\\n')"
   ],
   "outputs": [],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-05T02:06:23.096423Z",
     "start_time": "2025-09-05T02:06:23.073309Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import pandas as pd\n",
    "\n",
    "data = pd.read_csv(data_file)\n",
    "data"
   ],
   "id": "9c0b69e2c2d16020",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "   NumRooms Alley  square   Price\n",
       "0       NaN  Pave  123.43  127500\n",
       "1       2.0   NaN  454.30  106000\n",
       "2       4.0   NaN  234.00  178100\n",
       "3       NaN   NaN     NaN  140000"
      ],
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>NumRooms</th>\n",
       "      <th>Alley</th>\n",
       "      <th>square</th>\n",
       "      <th>Price</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>NaN</td>\n",
       "      <td>Pave</td>\n",
       "      <td>123.43</td>\n",
       "      <td>127500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>454.30</td>\n",
       "      <td>106000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>4.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>234.00</td>\n",
       "      <td>178100</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>140000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "# 处理缺失值\n",
    "inputs, outputs = data[:, :2], data[:, 2]\n",
    "inputs = inputs.fillna(inputs.mean())\n",
    "inputs"
   ],
   "id": "ce3d84adb3ce7741"
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": [
    "# 对于类别值和离散值，将 NaN 处理为一个类别，可以将类别列进行拆分，设计成独热编码的形式\n",
    "inputs = pd.get_dummies(inputs, dummy_na=True) # dummy_na表示的就是是否将 nan 列拆分出来形成单独一列\n",
    "inputs"
   ],
   "id": "fd062a628faa958f"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-05T02:39:41.661685Z",
     "start_time": "2025-09-05T02:39:41.647343Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 删除缺失值最多的列\n",
    "na_columns = data.isna()\n",
    "missing_counts = data.isna().sum() # sum函数默认就是按照列进行统计\n",
    "print(missing_counts)\n",
    "max_val = data.isna().sum(axis=0).max()\n",
    "print(missing_counts[missing_counts == max_val])\n",
    "drop_col = missing_counts[missing_counts == max_val].index.tolist()\n",
    "drop_col"
   ],
   "id": "6842b94e22a6c008",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "NumRooms    2\n",
      "Alley       3\n",
      "square      1\n",
      "Price       0\n",
      "dtype: int64\n",
      "Alley    3\n",
      "dtype: int64\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "['Alley']"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-05T02:16:33.435355Z",
     "start_time": "2025-09-05T02:16:33.421815Z"
    }
   },
   "cell_type": "code",
   "source": "df = data.drop(columns=drop_col)",
   "id": "784cfb7c338e121e",
   "outputs": [],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-05T03:06:04.781267Z",
     "start_time": "2025-09-05T03:06:04.769924Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 在向量或者矩阵中，默认单个向量就是列向量，但是在深度学习的张量中，通常将数据样本看作行向量\n",
    "import torch\n",
    "# 点积表示的是向量和向量之间 相同位置元素按元素乘积取和的结果\n",
    "x = torch.tensor([1, 2, 3, 4], dtype=torch.float32)\n",
    "y = torch.ones_like(x)\n",
    "print(x, y, torch.dot(x, y))\n",
    "# 矩阵和向量点积表示矩阵的每个行向量和向量进行点积，最后构成的一个列向量\n",
    "A = torch.arange(12, dtype=torch.float32).reshape((3, 4))\n",
    "print(A, x, torch.mv(A, x))\n",
    "# 矩阵和矩阵乘法就是矩阵 A 的每个行向量和矩阵 B 的每个列向量进行点积的结果\n",
    "B = torch.arange(12, dtype=torch.float32).reshape((3, 4))\n",
    "print(A, B, torch.mm(A.T, B))"
   ],
   "id": "3bac7a51662e1fa3",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([1., 2., 3., 4.]) tensor([1., 1., 1., 1.]) tensor(10.)\n",
      "tensor([[ 0.,  1.,  2.,  3.],\n",
      "        [ 4.,  5.,  6.,  7.],\n",
      "        [ 8.,  9., 10., 11.]]) tensor([1., 2., 3., 4.]) tensor([ 20.,  60., 100.])\n",
      "tensor([[ 0.,  1.,  2.,  3.],\n",
      "        [ 4.,  5.,  6.,  7.],\n",
      "        [ 8.,  9., 10., 11.]]) tensor([[ 0.,  1.,  2.,  3.],\n",
      "        [ 4.,  5.,  6.,  7.],\n",
      "        [ 8.,  9., 10., 11.]]) tensor([[ 80.,  92., 104., 116.],\n",
      "        [ 92., 107., 122., 137.],\n",
      "        [104., 122., 140., 158.],\n",
      "        [116., 137., 158., 179.]])\n"
     ]
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "",
   "id": "35c43da0d0a353cf"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-05T03:12:17.188923Z",
     "start_time": "2025-09-05T03:12:17.182257Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 范数是将向量映射到标量的函数, 通常满足缩放性、三角不等式和非负性\n",
    "# l1范数指的是向量元素绝对值求和\n",
    "u = torch.tensor([3, 4], dtype=torch.float32)\n",
    "torch.abs(u).sum()"
   ],
   "id": "3e84e83d18d0d010",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor(7.)"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 17
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-05T03:12:28.618845Z",
     "start_time": "2025-09-05T03:12:28.611628Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# l2范数指的是向量元素平方和的平方根\n",
    "torch.norm(u)"
   ],
   "id": "e3688779aeb61c14",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor(5.)"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 18
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-05T03:15:17.907923Z",
     "start_time": "2025-09-05T03:15:17.898824Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# Frobenius范数指的是矩阵的范数，值为矩阵元素平方和的平方根，满足向量范数的所有性质\n",
    "torch.norm(torch.ones(4, 9))"
   ],
   "id": "c60286bdef22cba",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor(6.)"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 19
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-05T03:23:07.554977Z",
     "start_time": "2025-09-05T03:23:07.547650Z"
    }
   },
   "cell_type": "code",
   "source": [
    "C = torch.arange(25, dtype=torch.float32).reshape((5, 5))\n",
    "D = C + C.T\n",
    "print(C, C.T)\n",
    "print(D == D.T)"
   ],
   "id": "240c619260127de3",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[ 0.,  1.,  2.,  3.,  4.],\n",
      "        [ 5.,  6.,  7.,  8.,  9.],\n",
      "        [10., 11., 12., 13., 14.],\n",
      "        [15., 16., 17., 18., 19.],\n",
      "        [20., 21., 22., 23., 24.]]) tensor([[ 0.,  5., 10., 15., 20.],\n",
      "        [ 1.,  6., 11., 16., 21.],\n",
      "        [ 2.,  7., 12., 17., 22.],\n",
      "        [ 3.,  8., 13., 18., 23.],\n",
      "        [ 4.,  9., 14., 19., 24.]])\n",
      "tensor([[True, True, True, True, True],\n",
      "        [True, True, True, True, True],\n",
      "        [True, True, True, True, True],\n",
      "        [True, True, True, True, True],\n",
      "        [True, True, True, True, True]])\n"
     ]
    }
   ],
   "execution_count": 25
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-05T03:26:03.379964Z",
     "start_time": "2025-09-05T03:26:03.373871Z"
    }
   },
   "cell_type": "code",
   "source": [
    "X = torch.arange(24, dtype=torch.float32).reshape(2, 3, 4)\n",
    "print(X, len(X))\n",
    "Y = torch.tensor([1, 2, 3, 4], dtype=torch.float32)\n",
    "print(Y, len(Y))"
   ],
   "id": "fa4b1ccab850825a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[[ 0.,  1.,  2.,  3.],\n",
      "         [ 4.,  5.,  6.,  7.],\n",
      "         [ 8.,  9., 10., 11.]],\n",
      "\n",
      "        [[12., 13., 14., 15.],\n",
      "         [16., 17., 18., 19.],\n",
      "         [20., 21., 22., 23.]]]) 2\n",
      "tensor([1., 2., 3., 4.]) 4\n"
     ]
    }
   ],
   "execution_count": 27
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-05T03:31:25.800889Z",
     "start_time": "2025-09-05T03:31:25.794075Z"
    }
   },
   "cell_type": "code",
   "source": [
    "Sum_A = A.sum(axis=1)\n",
    "print(A, Sum_A)\n",
    "print(A, A.T / Sum_A)"
   ],
   "id": "396d72f45de36bb",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[ 0.,  1.,  2.,  3.],\n",
      "        [ 4.,  5.,  6.,  7.],\n",
      "        [ 8.,  9., 10., 11.]]) tensor([ 6., 22., 38.])\n",
      "tensor([[ 0.,  1.,  2.,  3.],\n",
      "        [ 4.,  5.,  6.,  7.],\n",
      "        [ 8.,  9., 10., 11.]]) tensor([[0.0000, 0.1818, 0.2105],\n",
      "        [0.1667, 0.2273, 0.2368],\n",
      "        [0.3333, 0.2727, 0.2632],\n",
      "        [0.5000, 0.3182, 0.2895]])\n"
     ]
    }
   ],
   "execution_count": 34
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
